<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/20
 * Time: 13:31
 */

namespace app\modules\setting\controllers\seller;


use app\common\controllers\AdminOptionsController;
use app\common\helper\CommonHelper;
use app\models\EtcSellerTiktokProduct;
use app\models\EtcSellerTiktokProductEdit;
use app\models\QlUserTiktokProductGoods;
use app\modules\setting\configs\seller\ProductConfig;
use app\modules\setting\models\cpgl\QlUserTiktokProductGoodsSearch;
use sookie\auth\models\Adminuser;
use sookie\db\DBHandler;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use Yii;

class ProductController extends AdminOptionsController
{
    /**
     * @return array
     * 列表数据
     */
    public function actionIndex()
    {
        try {
            $searchModel = new QlUserTiktokProductGoodsSearch();
            $dataProvider = $searchModel->searchSeller(Yii::$app->request->queryParams);
            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $re = $item;
                    if(is_object($item)){
                        $re = $item->toArray();
                    }
                    $re['name'] = CommonHelper::getProductName($item);
                    return $re;
                },$data);
            }
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],ProductConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
     * @param $id
     * @return array
     * 修改接口
     */
    public function actionUpdate($id)
    {
        try {
            CommonHelper::notSellerThrow();
            $info = Adminuser::getUserInfo();
            $model = EtcSellerTiktokProductEdit::find()->andWhere(['user_id'=>$info->id,'goods_id'=>$id])->one();

            if (Yii::$app->request->isPost){
                if(empty($model)){
                    $model = new EtcSellerTiktokProductEdit();
                    $model->user_id = $info->id;
                    $model->goods_id = $id;
                }

                if ($model->load(Yii::$app->request->post(),"") && DBHandler::adjustFieldLength(EtcSellerTiktokProductEdit::tableName(),Yii::$app->request->post()) && $model->save()) {
                    return ["msg" => "修改成功"];
                }else{
                    return ["ok" => false,"msg" => CommonHelper::getFirstErrorStr($model->errors),"code" => 401];
                }
            }
            return ProductConfig::configUpdate($id,$model);
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
     * @param $id
     * @return array
     * 修改接口
     */
    public function actionSubmit($id)
    {
        $tran = Yii::$app->db->beginTransaction();
        try {
            CommonHelper::notSellerThrow();
            $info = Adminuser::getUserInfo();
            $model = EtcSellerTiktokProductEdit::find()->andWhere(['user_id'=>$info->id,'goods_id'=>$id])->one();

            if (Yii::$app->request->isPost){
                if(empty($model)){
                    $model = new EtcSellerTiktokProductEdit();
                    $model->user_id = $info->id;
                    $model->goods_id = $id;
                }

                if ($model->load(Yii::$app->request->post(),"") && DBHandler::adjustFieldLength(EtcSellerTiktokProductEdit::tableName(),Yii::$app->request->post()) && $model->save()) {
                    $ql_product = QlUserTiktokProductGoods::find()->andWhere(['id'=> $id])->one();
                    if(!$ql_product instanceof QlUserTiktokProductGoods){
                        throw new Exception("产品不存在");
                    }
                    $product_model = new EtcSellerTiktokProduct();
                    $product_model->load($ql_product->toArray(),'');
                    $product_model->load($model->toArray(),'');
                    $product_model->user_id = $info->id;
                    $product_model->goods_id = $id;
                    if(!$product_model->save()){
                        throw new Exception(CommonHelper::getFirstErrorStr($product_model->errors));
                    }

                    $tran->commit();
                    return ["msg" => "提报成功"];
                }else{
                    return ["ok" => false,"msg" => CommonHelper::getFirstErrorStr($model->errors),"code" => 401];
                }
            }
            return ProductConfig::configSubmit($id,$model);
        }catch (\Throwable $e){
            $tran->rollBack();
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    protected function findModel($id)
    {
        return 1;
    }
}